var dateUtil = require('../../utils/dateUtil.js');


const app = getApp();
Page({
    data: {
        StatusBar: app.globalData.StatusBar,
        CustomBar: app.globalData.CustomBar,
        index: null,
        multiIndex: [0, 0, 0],
        // date: formatTime(new Date()),
        date: dateUtil.formatDate(new Date()),
        time: dateUtil.formatTime2(new Date()),
        region: ['广东省', '广州市', '海珠区'],
        imgList: [],
        modalName: null,
        modalName2: null,
        textareaAValue: '',
        textareaBValue: '',
        labelId: 1,
        userId: 18721260590,
        timeList: [],
        typeList: [],
        timeListTypeId: null
    },

    showModal(e) {
        this.setData({
            modalName: e.currentTarget.dataset.target
        })
    },
    hideModal(e) {
        this.setData({
            modalName: null
        })
    },
    PickerChange(e) {
        console.log(this.labelId);
        console.log(this.data.typeList[e.detail.value]);
        this.data.labelId = this.data.typeList[e.detail.value].id
        console.log(this.data.labelId);
        this.setData({
            index: e.detail.value
        })

    },
    MultiChange(e) {
        this.setData({
            multiIndex: e.detail.value
        })
    },
    TimeChange(e) {
        this.setData({
            time: e.detail.value
        })
    },
    DateChange(e) {
        this.setData({
            date: e.detail.value
        })
    },
    ChooseImage() {
        wx.chooseImage({
            count: 4, //默认9
            sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album'], //从相册选择
            success: (res) => {
                if (this.data.imgList.length != 0) {
                    this.setData({
                        imgList: this.data.imgList.concat(res.tempFilePaths)
                    })
                } else {
                    this.setData({
                        imgList: res.tempFilePaths
                    })
                }
            }
        });
    },
    ViewImage(e) {
        wx.previewImage({
            urls: this.data.imgList,
            current: e.currentTarget.dataset.url
        });
    },
    DelImg(e) {
        wx.showModal({
            title: '召唤师',
            content: '确定要删除这段回忆吗？',
            cancelText: '再看看',
            confirmText: '再见',
            success: res => {
                if (res.confirm) {
                    this.data.imgList.splice(e.currentTarget.dataset.index, 1);
                    this.setData({
                        imgList: this.data.imgList
                    })
                }
            }
        })
    },
    textareaAInput(e) {
        this.setData({
            textareaAValue: e.detail.value
        })
    },
    textareaBInput(e) {
        this.setData({
            textareaBValue: e.detail.value
        })
    },
    formSubmit(e) {
        const that = this;
        console.log('this.date', this.data.date)
        console.log('this.text', this.data.text)
        console.log('this.labelId', this.data.labelId)
        console.log('this.userId', this.data.userId)
        wx.request({
            // url: 'https://calamar.top:8081/timeList/insert', //仅为示例，并非真实的口址
            url: 'https://127.0.0.1:8081/timeList/insert',
            method: 'post',
            data: {
                userId: 18721260590,
                dateTime: this.data.date,
                text: this.data.textareaAValue,
                labelId: this.data.labelId,
                type: 1 //暂时就这一种
            },
            header: {
                'content-type': 'application/json' // 默认值
            },
            success(res) {
                console.log(res.data)
                if (res.data.code === "000000") {
                    wx.showModal({
                        title: '温馨提示',
                        content: '添加成功',
                        showCancel: false,//是否显示取消按钮
                        cancelText: "否",//默认是“取消”
                        cancelColor: 'red',//取消文字的颜色
                        confirmText: "确定",//默认是“确定”
                        confirmColor: 'pink',//确定文字的颜色
                        success: function (res) {
                            // wx.navigateTo({
                            //   url: '/pages/timelist/index',
                            // })
                            // wx.navigateBack({
                            //     delta: 0,
                            // })
                            that.onLoad(that.options)
                            that.hideModal()
                        },
                        fail: function (res) {
                        },//接口调用失败的回调函数
                        complete: function (res) {
                        },//接口调用结束的回调函数（调用成功、失败都会执行）
                    })
                } else {
                    wx.showModal({
                        title: '提示', content: res.data.description,
                        showCancel: false,//是否显示取消按钮
                        cancelText: "否",//默认是“取消”
                        cancelColor: 'red',//取消文字的颜色
                        confirmText: "确定",//默认是“确定”
                        confirmColor: 'pink',//确定文字的颜色
                    })
                }
            }
        })
    },

    onLoad: function (options) {

        let that = this;

        let data = {
            "userId": 18721260590
        }
        wx.request({
            // url: 'https://calamar.top:8081/timeList/list',
            url: 'https://127.0.0.1:8081/timeList/list',
            method: 'POST',
            data: data,    //参数为键值对字符串
            header: {
                //设置参数内容类型为x-www-form-urlencoded
                'content-type': 'application/json'
            },
            success: function (res) {
                console.log(res.data)
                that.setData({
                    timeList: res.data.data,
                })
            }
        })

        wx.request({
            // url: 'https://calamar.top:8081/timeList/labelList',
            url: 'https://127.0.0.1:8081/timeList/labelList',
            method: 'POST',
            data: data,    //参数为键值对字符串
            header: {
                //设置参数内容类型为x-www-form-urlencoded
                'content-type': 'application/json'
            },
            success: function (res) {
                console.log(res.data),
                    that.setData({
                        typeList: res.data.data,
                        index: 0,
                        labelId: res.data.data[0].id
                    })
            }
        })

    },
})

